package com.maxlen.goods.controller;

import com.maxlen.common.utils.PageUtils;
import com.maxlen.common.utils.R;
import com.maxlen.goods.entity.SpuInfoEntity;
import com.maxlen.goods.entity.vo.SpuInfoPageParamsVO;
import com.maxlen.goods.entity.vo.spuAdd.SpuSaveVO;
import com.maxlen.goods.service.SpuInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.Arrays;


/**
 * spu信息
 *
 * @author MaxLen
 * @email MaxLen@gmail.com
 * @date 2022-10-27 10:12:58
 */
@RestController
@RequestMapping("goods/spuinfo")
public class SpuInfoController {
    @Autowired
    private SpuInfoService spuInfoService;

    /**
     * 获取所有上架的 SPU，只返回 id 和 名称
     * @return
     */
    @GetMapping("/getSpuAll")
    public R getSpuAll(){
        return spuInfoService.getSpuAll();
    }

    @GetMapping("/getWeight/{skuId}")
    public R getWeight(@PathVariable("skuId") Long skuId){
        BigDecimal weight = spuInfoService.getWeightBySkuId(skuId);
        return R.ok().setData(weight);
    }

    /**
     * 列表
     */
    @PostMapping("/list")
    // @RequiresPermissions("goods:spuinfo:list")
    public R list(@RequestBody SpuInfoPageParamsVO params){
        PageUtils page = spuInfoService.queryPage(params);
        return R.ok().put("page", page);
    }

    /**
     * 上架商品
     */
    @GetMapping("/up/{spuId}")
    // @RequiresPermissions("goods:spuinfo:list")
    public R list(@PathVariable("spuId") Long spuId){
        return spuInfoService.updateByPublishStatus(spuId);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    // @RequiresPermissions("goods:spuinfo:info")
    public R info(@PathVariable("id") Long id){
		SpuInfoEntity spuInfo = spuInfoService.getById(id);

        return R.ok().put("spuInfo", spuInfo);
    }

    /**
     * SPU发布上传保存
     */
    @PostMapping("/save")
    // @RequiresPermissions("goods:skuinfo:save")
    public R save(@RequestBody SpuSaveVO spuSaveVO){
        spuInfoService.saveAllDetail(spuSaveVO);
        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    // @RequiresPermissions("goods:spuinfo:update")
    public R update(@RequestBody SpuInfoEntity spuInfo){
		spuInfoService.updateById(spuInfo);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    // @RequiresPermissions("goods:spuinfo:delete")
    public R delete(@RequestBody Long[] ids){
		spuInfoService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

}
